import axios from "axios";
import { get, post } from "./index";

export interface CreateMarkTaskParams {
    dataSetId: number;
    name: string;
}

export type UpdateMarkTaskParams = CreateMarkTaskParams & {
    id: number;
};

// 创建标注
export const createMarkTask = async (params: CreateMarkTaskParams) => {
    const res = await post("/marktask/create", params);
    return res;
};

// 删除标注
export const deleteMarkTask = async (id: number) => {
    const res = await post("/marktask/delete", {
        id,
    });
    return res;
};

// 对文件进行标注
export const markFile = async (id: number, taskId: number) => {
    const res = await post("/marktask/file/mark", {
        id,
        taskId,
    });
    return res.data?.fileUrls || [];
};

// 获取标注任务列表
export interface TaskQueryParams {
    name: string;
    page: number;
    pageSize: number;
}
export interface MasktaskItem {
    dataSetId: number;
    id: number;
    name: string;
    needMarkStatus: number;
    preMarkStatus: number;
}
export const getMarkTaskList = async (params: TaskQueryParams) => {
    const res = await post("/marktask/list", params);
    const data = res.data || {};
    const list: MasktaskItem[] = data.maskTaskListDtoList || [];
    return {
        total: data.total,
        list,
    };
};

// 更新标注任务
export const updateMarkTask = async (params: UpdateMarkTaskParams) => {
    const res = await post("/marktask/update", params);
    return res;
};

// 更新标注任务是否完成预标注状态
export interface NeedMarkStatusParams {
    id: number;
    needMarkStatus: number;
}
export const updateNeedMarkStatus = async (params: NeedMarkStatusParams) => {
    const res = await post("/marktask/updateNeedMarkStatus", params);
    return res;
};

// 更新标注任务是否完成预标注状态
export interface PreMarkStatusParams {
    id: number;
    preMarkStatus: number;
}
export const updatePreMarkStatus = async (params: PreMarkStatusParams) => {
    const res = await post("/marktask/updatePreMarkStatus", params);
    return res;
};

// 上传标注文件
export const uploadMarkFile = async (files: File[], dataSetFileId: number) => {
    const res = await post(
        `/marktask/uploadMarkFile?dataSetFileId=${dataSetFileId}`,
        {
            files,
        },
        {
            headers: {
                "Content-Type": "multipart/form-data",
            },
        },
    );
    return res;
};

// 桌面标注软件相关
// 心跳检测接口
export const getHeart = async () => {
    const res = await axios.get("http://127.0.0.1:56123/snap-plugin/api/v1/heart");
    return res.data;
};

// 下载文件提交接口
interface FileUrl {
    resUrl: string;
    fileName: string;
    category: number;
}
export interface DownloadFileParams {
    storeDir: string;
    openSnap: boolean;
    fileUrls: FileUrl[];
}
export const submitDownloadFile = async (params: DownloadFileParams) => {
    const res = await axios.post(
        "http://127.0.0.1:56123/snap-plugin/api/v1/download/submit",
        params,
    );
    return res.data;
};

// 下载任务查询接口
export interface DownloadFile {
    resUrl: string;
    fileName: string;
    category: number;
    progress: number;
    speed: number;
    avgSpeed: number;
    localPath: string;
}
export interface DownloadTaskResult {
    taskId: number;
    storeDir: string;
    openSnap: boolean;
    status: number;
    files: DownloadFile[];
    message: string;
}
export const queryDownloadTask = async (taskId: number) => {
    const res = await axios.post("http://127.0.0.1:56123/snap-plugin/api/v1/download/query", {
        taskId,
    });
    const data: DownloadTaskResult = res.data?.data || {};
    return data;
};
// 更新标注软件路径
export const updatePluginPath = async (snapPath: string) => {
    const res = await axios.post("http://127.0.0.1:56123/snap-plugin/api/v1/itk-snap/path/update", {
        snapPath,
    });
    return res.data;
};

// 获取人工标注列表
export interface MaskTaskFileItem {
    id: number;
    name: string;
    size: number;
    updateTime: string;
}
export const getMaskFileList = async (params: TaskQueryParams) => {
    const res = await post("/marktask/maskFile/list", params);
    const data = res.data || {};
    const list: MaskTaskFileItem[] = data.maskFileListDtoList || [];
    return {
        total: data.total,
        list,
    };
};

// 获取预标注列表
export const getPreMaskFileList = async (params: TaskQueryParams) => {
    const res = await post("/marktask/preMaskFile/list", params);
    const data = res.data || {};
    const list: MaskTaskFileItem[] = data.preMaskFileListDtoList || [];
    return {
        total: data.total,
        list,
    };
};

// 上传标签文件
export const uploadLabelFileList = async (id: number, files: File) => {
    // const formData = new FormData();
    // formData.append("id", id);
    // for (const file of files) {
    //     formData.append("file", file);
    // }
    // const res = await post("/marktask/label_file/upload", formData, {
    //     headers: {
    //         "Content-Type": "multipart/form-data",
    //     },
    // });

    const res = await post(
        "/marktask/label_file/upload",
        {
            id,
            file: files,
        },
        {
            headers: {
                "Content-Type": "multipart/form-data",
            },
        },
    );
    return res;
};
// 人工标注文件夹上传
export const uploadMarkFileList = async (id: number, files: File[]) => {
    const formData = new FormData();
    formData.append("id", id);
    for (const file of files) {
        formData.append("folder", file);
    }
    const res = await post("/marktask/uploadMaskFileFolder", formData, {
        headers: {
            "Content-Type": "multipart/form-data",
        },
    });
    return res;
};
// 预标注文件夹上传
export const uploadPreFileList = async (id: number, files: File[]) => {
    const formData = new FormData();
    formData.append("id", id);
    for (const file of files) {
        formData.append("folder", file);
    }
    const res = await post("/marktask/uploadPreMaskFolder", formData, {
        headers: {
            "Content-Type": "multipart/form-data",
        },
    });
    return res;
};

// 删除预标注文件
export const deletePreMaskFile = async (fileId: number, maskTaskId: number) => { 
    const res = await post("/marktask/delPreMaskFile", {
        fileId,
        maskTaskId,
    });
    return res;
};

// 删除人工标注文件
export const deleteMaskFile = async (fileId: number, maskTaskId: number) => {
    const res = await post("/marktask/delMaskFile", {
        fileId,
        maskTaskId,
    });
    return res;
};
